package com.rc.pojo.entity;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.AbstractJdbcDialect;
import com.mybatisflex.core.util.StringUtil;
import com.zaxxer.hikari.HikariDataSource;

public class Codegen {

   static class MyDialect extends AbstractJdbcDialect {

       @Override
       protected String forBuildColumnsSql(String schema, String tableName) {
           String s = "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + ".\"" : "") + tableName + "\" WHERE 1 = 2";
           System.out.println(s);
           return s;
       }
    }

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("dm.jdbc.driver.DmDriver");
        dataSource.setJdbcUrl("jdbc:dm://192.168.123.80:30236");
        dataSource.setUsername("SYSDBA");
        dataSource.setPassword("SYSDBA001");


        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        globalConfig.setGenerateSchema("RC");
        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig,new MyDialect());
        dataSource.setSchema("RC");

        //生成代码

        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setUnGenerateTable("COMMENT");
        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.rc.pojo.po");
        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setWithBasePackage("po")
                .setJdkVersion(17);


        //设置生成 mapper
//

//        globalConfig.enableController();
//
//        globalConfig.enableTableDef();

        globalConfig.setSourceDir("F:\\Java\\rural-classroom\\rc-pojo\\src\\main\\java");
        return globalConfig;
    }
}
